package cn.lhf.cd.controller;

import cn.lhf.cd.annotation.FXController;
import cn.lhf.cd.service.UserService;
import javafx.scene.control.Label;

import java.math.BigDecimal;
import java.net.URL;
import java.util.ResourceBundle;

@FXController(path = "/ui/withdrawal.fxml")
public class WithdrawalController extends ChildrenController {
    public Label tipLabel;

    @Override
    public String getTitle() {
        return "现金提取窗口";
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @Override
    public void onBtnLeft1() {
        doWithdrawal("500");
    }

    @Override
    public void onBtnLeft2() {
        doWithdrawal("1000");
    }

    @Override
    public void onBtnLeft3() {
        doWithdrawal("1500");
    }

    @Override
    public void onBtnLeft4() throws Exception {
        getMainController().setFrameView(WithdrawalOtherController.class);
    }

    @Override
    public void onBtnRight1() {
        doWithdrawal("100");
    }

    @Override
    public void onBtnRight2() {
        doWithdrawal("200");
    }

    @Override
    public void onBtnRight3() {
        doWithdrawal("300");
    }

    @Override
    public void onBtnRight4() {
        getMainController().goHome();
    }

    /**
     * 取款
     * @param num 数额
     */
    private void doWithdrawal(String num) {
        try {
            UserService.withdraw(getMainController().getSelfId(), new BigDecimal(num));
            getMainController().setFrameView(SuccessController.class, "现金提取成功，%d秒后将返回主界面");
        } catch (Exception e) {
            sendTip(tipLabel, e.getMessage());  //出错了
        }
        clearTips();
    }

}
